var layer, table, form;
window.onload = function () {
    setTimeout(function () {
        getStuInfo();
    }, 600);
}

layui.use(['layer', 'laydate', 'table', 'form'], function () {
    layer = layui.layer;
    laydate = layui.laydate;
    table = layui.table;
    form = layui.form;

    laydate.render({
        elem: '#grade',
        type: 'year',
        trigger: 'click',
        // done: function(value, date){
        //     //console.log(value); //得到日期生成的值，如：2017-08-18
        //     //console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
        //     // 获取班级下拉列表
        //     //getClasses(value);
        // }
    });


    // 监听院系下拉列表
    form.on("select(department)", function (data) {
        // 获取专业下拉列表
        getMajorByDeptId(data.value);
    });

    // 监听专业下拉列表
    form.on("select(major)", function (data) {
        // 获取班级下拉列表
        getClazzByMajorId(data.value);
    });

    //自定义验证规则
    form.verify({
        stuId: function (value) {
            if (value == '') {
                return '请填写学号';
            }
        },
        stuName: function (value) {
            if (value == '') {
                return '请填写姓名';
            }
        },
        sex: function (value) {
            if (value == '') {
                return '请选择性别';
            }
        },
        birthplace: function (value) {
            if (value == '') {
                return '请填写籍贯';
            }
        },
        birthday: function (value) {
            if (value == '') {
                return '请填写出生年月';
            }
        },
        phoneNumber: function (value) {
            if (value == '') {
                return '请填写联系方式';
            }
        },
        kinsfolkName: function (value) {
            if (value == '') {
                return '请填写家属姓名';
            }
        },
        kinsfolkPhoneNumber: function (value) {
            if (value == '') {
                return '请填写家属联系方式';
            }
        },
        department: function (value) {
            if (value == '') {
                return '请选择系部';
            }
        },
        major: function (value) {
            if (value == '') {
                return '请选择专业';
            }
        },
        clazz: function (value) {
            if (value == '') {
                return '请选择专业';
            }
        },
        grade: function (value) {
            if (value == '') {
                return '请选择年级';
            }
        },
        dormitory: function (value) {
            if (value == '') {
                return '请选择宿舍';
            }
        }
    });
    //监听提交
    form.on('submit(form)', function (data) {
        var param = data.field;
        modifyStuInfo(param);// 修改学生信息
        return false;
    });
});

/**
 * 三级联动，获取学生信息并自动填充
 */
function getStuInfo() {
    // 获取表单参数
    var stuId = $("#stuId").val();
    var param = {
        stuId: stuId
    }
    if (!$.isEmptyObject(param.stuId)) {
        $.ajax({
            type: "POST",
            url: "/student/getStuInfoById",
            //contentType: "application/json",
            sync: true,
            data: param,
            dataType: "json",
            success: function (result) {
                console.log(result)
                if (result.code == 200) {
                    var stu = result.data;
                    $("#stuName").val(stu.stuName);
                    getAllDept(stu.deptId);
                    getMajorByDeptId(stu.majorId);
                    getClazzByMajorId(stu.clazzId);
                    getAllDormitory(stu.dormitoryId);
                    // debugger
                    if (stu.sex == "男") {
                        $('#man').prop("checked", true);
                        $('#female').prop("checked", false);
                    } else {
                        $('#female').prop("checked", true);
                        $('#man').prop("checked", false);
                    }
                    form.render();
                } else {
                    layer.msg('该学生不存在！', {icon: 2, offset: '100px', time: 2000});
                }
            }
        });
    }
}

/**
 * 三级联动，获取学生信息并自动填充
 */
function modifyStuInfo(param) {
    param.clazzId = param.clazz;
    param.dormitoryId = param.dormitory;
    $.ajax({
        type: "POST",
        url: "/student/modifyStuInfo",
        contentType: "application/json",
        sync: true,
        data: JSON.stringify(param),
        dataType: "json",
        success: function (result) {
            console.log(result)
            if (result.code == 200) {
                // 弹出提示
                layer.msg('修改信息成功！', {anim: 3, icon: 6, time: 2000, shade: 0.1});
                setTimeout(function () {
                    // 关闭自身窗口
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                    // 刷新父窗口
                    parent.layui.table.reload('studentTable');//重新加载父页面中的数据表格
                }, 1000);
            } else {
                layer.msg(result.msg, {icon: 2, offset: '100px', time: 2000});
            }
        }
    });
}

/**
 * 获取所有宿舍
 * @param dormitoryId
 */
function getAllDormitory(dormitoryId) {
    param = {
        dormitoryId: ""
    }
    $.ajax({
        type: "POST",
        url: "/dormitory/getAllDormitory",
        // contentType: "application/json",
        sync: true,
        data: param,
        dataType: "json",
        success: function (result) {
            $("#dormitory").empty();
            $("#dormitory").append("<option value=''>请选择宿舍</option>");
            if (result.code == 200) {
                $.each(result.data, function (i, o) {
                    var opt = '';
                    if (dormitoryId === o.dormitoryId) {
                        opt = '<option value=' + o.dormitoryId + ' selected>' + o.dormitoryName + '</option>';
                    } else {
                        opt = '<option value=' + o.dormitoryId + '>' + o.dormitoryName + '</option>';
                    }
                    $("#dormitory").append(opt);
                });
            }
            // 渲染页面表单，否则不显示数据
            layui.form.render('select');
        }
    });
}

/**
 * 获取所有院系
 * @param deptId
 */
function getAllDept(deptId) {
    $.ajax({
        type: "POST",
        url: "/common/getAllDept",
        contentType: "application/json",
        sync: true,
        data: {},
        dataType: "json",
        success: function (result) {
            $("#department").empty();
            $("#department").append("<option value=''>请选择院系</option>");
            if (result.code == 200) {
                $.each(result.data, function (i, o) {
                    var opt = '';
                    if (deptId === o.deptId) {
                        opt = '<option value=' + o.deptId + ' selected>' + o.deptName + '</option>';
                    } else {
                        opt = '<option value=' + o.deptId + '>' + o.deptName + '</option>';
                    }
                    $("#department").append(opt);
                });
            }
            // 渲染页面表单，否则不显示数据
            layui.form.render('select');
        }
    });
}

/**
 * 根据院系获取专业
 * @param majorId
 */
function getMajorByDeptId(majorId) {
    // 获取表单参数
    var deptId = $("#department option:selected").val();
    var param = {
        deptId: deptId
    }
    // 校验成功，执行添加请求
    $.ajax({
        type: "POST",
        url: "/common/getMajorByDeptId",
        // contentType: "application/json",
        sync: true,
        data: param,
        dataType: "json",
        success: function (result) {
            $("#major").empty();
            $("#major").append("<option value=''>请选择专业</option>");
            if (result.code == 200) {
                $.each(result.data, function (i, o) {
                    var opt = '';
                    if (majorId === o.majorId) {
                        opt = '<option value=' + o.majorId + ' selected>' + o.majorName + '</option>';
                    } else {
                        opt = '<option value=' + o.majorId + '>' + o.majorName + '</option>';
                    }
                    $("#major").append(opt);
                });
            }
            // 渲染页面表单，否则不显示数据
            layui.form.render('select');
        }
    });
}

/**
 * 根据专业获取班级
 * @param clazzId
 */
function getClazzByMajorId(clazzId) {
    // 获取表单参数
    var majorId = $("#major").val();
    var param = {
        majorId: majorId
    }
    // 校验成功，执行添加请求
    $.ajax({
        type: "POST",
        url: "/common/getClazzByMajorId",
        // contentType: "application/json",
        sync: true,
        data: param,
        dataType: "json",
        success: function (result) {
            $("#clazz").empty();
            $("#clazz").append("<option value=''>请选择班级</option>");
            if (result.code == 200) {
                $.each(result.data, function (i, o) {
                    var opt = '';
                    if (clazzId === o.clazzId) {
                        opt = '<option value=' + o.clazzId + ' selected>' + o.clazzName + '</option>';
                    } else {
                        opt = '<option value=' + o.clazzId + '>' + o.clazzName + '</option>';
                    }
                    $("#clazz").append(opt);
                });

            }
            // 渲染页面表单，否则不显示数据
            layui.form.render('select');
        }
    });
}